﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Collections;
using System.Text;
using System.Text.RegularExpressions;
using System.Data;
using System.Data.SqlClient;

namespace IETTortak
{
    public class GuzergahParser
    {
        static Regex reGuzergah = new Regex(@"<center><b><font size=2>(.*?)</font size=2><p>\s*(?<hatName>[^<]*\s)</b>\s*hatt.<br>\D*(?<hatTime>\d*?)\sdakika<br>(?<other>.*?)\s*<a href.*?align=center\s*>(?<stop>.*?)</table>.*?G.ncelleme Tarihi:</i>.*?</font>",
            RegexOptions.Singleline);
        // hatName
        // hatTime
        // stop
        static Regex reStop = new Regex(@"(?<stop><tr bgcolor.*?</td><td>\s*(?<stopName>.*?)\s*<font color=gray>(?<stopLocation>.*?)<font></td>)+?",
            RegexOptions.Singleline);

        public static Guzergah Parse(string guzHtml)
        {
            Match m = reGuzergah.Match(guzHtml);
            if (m.Success)
            {
                //if (m.Groups["hatName"].Value.Trim().Length == 0)
                  //  return null;

                Guzergah ret = new Guzergah(m.Groups["hatName"].Value, int.Parse(m.Groups["hatTime"].Value));
                string s = m.Groups["stop"].Value;

                MatchCollection mc = reStop.Matches(s);
                foreach (Match mm in mc)
                {
                    ret.AddStop(mm.Groups["stopName"].Value, mm.Groups["stopLocation"].Value);
                }

                return ret;
            }
            throw new ArgumentException("Guzergah.Parse: Regex cannot match to the input");
        }
    }

    public class Guzergah
    {
        private string hatName;

        public string HatName
        {
            get { return hatName; }
            set { hatName = value; }
        }
        private int hatTime;

        public int HatTime
        {
            get { return hatTime; }
            set { hatTime = value; }
        }

        public struct Stop
        {
            public string Name;
            public string Location;
            public Stop(string Name, string Location)
            {
                this.Name = Name;
                this.Location = Location;
            }
        }

        private Queue<Stop> stop;



        public Queue<Stop> Duraklar
        {
            get { return stop; }
        }

        public Guzergah(string hatName, int hatTime)
        {
            this.hatName = hatName;
            this.hatTime = hatTime;
            this.stop = new Queue<Stop>();
        }

        public void AddStop(string stopName, string stopLocation)
        {
            stop.Enqueue(new Stop(stopName, stopLocation));
        }
    }

    public class GuzergahDatabaseHelper
    {
        public delegate void UpdateProgressHandler(string key, int tamamlanan, int tümü);
        public event UpdateProgressHandler UpdateProgress;

        protected void OnUpdateProgress(string key, int tamamlanan, int tümü)
        {
            if (UpdateProgress != null)
                UpdateProgress(key, tamamlanan, tümü);
        }

        private string key;
        public GuzergahDatabaseHelper(string key)
        {
            this.key = key;
        }

        public void AddGuzergahListToDB(List<Guzergah> lst)
        {
            int tamamlanan = 0;
            foreach (Guzergah g in lst)
            {                
                int routeID = int.Parse(DAL.ExecScalar("IF NOT EXISTS(SELECT * FROM ServiceInfo.tblRoute WHERE Name = @Name AND RouteTime = @RouteTime) \n begin \n INSERT INTO ServiceInfo.tblRoute (Name, RouteTime) VALUES (@Name, @RouteTime) \n SELECT SCOPE_IDENTITY() \n end \n else \n SELECT RouteID FROM ServiceInfo.tblRoute WHERE Name = @Name AND RouteTime = @RouteTime", false, "@Name", g.HatName, "@RouteTime", g.HatTime).ToString());

                int stopNo = 0;

                foreach (Guzergah.Stop s in g.Duraklar)
                {
                    int stopID = int.Parse(DAL.ExecScalar("IF NOT EXISTS(SELECT * FROM ServiceInfo.tblStop WHERE Name = @Name AND @Location = @Location) \n begin \n INSERT INTO ServiceInfo.tblStop (Name, Location) VALUES (@Name, @Location) \n SELECT SCOPE_IDENTITY() \n end \n else SELECT StopID FROM ServiceInfo.tblStop WHERE Name = @Name AND @Location = @Location", false, "@Name", s.Name, "@Location", s.Location).ToString());

                    DAL.ExecNonQuery("IF NOT EXISTS(SELECT * FROM ServiceInfo.tblRouteStop WHERE RouteID = @RouteID AND StopID = @StopID) \n begin \n INSERT INTO ServiceInfo.tblRouteStop (RouteID, StopID, StopNo) VALUES (@RouteID, @StopID, @StopNo) \n end", false, "@RouteID", routeID, "@StopID", stopID, "@StopNo", stopNo);
                    stopNo++;
                }
                tamamlanan++;
                OnUpdateProgress(key, tamamlanan, lst.Count);
            }
        }
    }
}
